package com.learner.course.service.impl;

import com.learner.common.exception.ExceptionUtil;
import com.learner.common.exception.RestException;
import com.learner.course.repository.DictionaryRepository;
import com.learner.course.service.DictionaryService;
import com.learner.pojo.course.dictionary.SysDictionary;
import com.learner.pojo.course.dictionary.SysDictionaryValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ice2020x
 * @Date: 2023/3/8
 * @Description: 字典
 **/
@Service
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    DictionaryRepository dictionaryRepository;

    @Override
    public SysDictionaryValue findDictionaryByType(String type) {
        String prefix = type.substring(0, 3);
        SysDictionary sysDictionary = dictionaryRepository.findBydType(prefix);
        List<SysDictionaryValue> dValue = sysDictionary.getDValue();
        SysDictionaryValue sysDictionaryValueItem = null;
        for (SysDictionaryValue sysDictionaryValue : dValue) {
            if (type.equals(sysDictionaryValue.getSdId())) {
                sysDictionaryValueItem = sysDictionaryValue;
            }
        }
        if (sysDictionaryValueItem == null) {
            throw ExceptionUtil.createException(RestException.class, 2000001);
        }
        return sysDictionaryValueItem;
    }

}
